from flask import Flask,render_template,json,request,jsonify,make_response
from Controller.Data_Controller import DataApi as d  

app = Flask(__name__)

@app.route('/api/<string:choice>',methods=['GET','POST'])
def getDailyIncome(choice):     
    if choice=='DailyIncome':           
        data=d.getDailyIncome(lag=int(request.form['lags']))['data']
    elif choice=='MonthlyIncome':
        data=d.getMonthlyIncome()['data']
    elif choice=='GoodsDailyIncome':        
        data=d.getGoodsDailyIncome(dt=request.form['dt'])['data']        
    elif choice == 'GoodsIncome':
        data=d.getGoodsTotalIncome()['data'] 
    elif choice == 'GoodsIncomeInorder':
        data=d.getGoodsTotalIncomeByOrder()['data']
    elif choice == 'DailyHourlyIncome':
        data=d.getDailyHourlyIncome(dt=request.form['dt'])['data']
    elif choice == 'UserDailyExpense':
        data=d.getUserDailyExpense(dt=request.form['dt'])
    elif choice == 'SomeUserDailyExpense':    
        data=d.getSomeUserDailyExpense(userID=request.form['userID'])        
    elif choice == 'UserTotalExpense':
        data=d.getUserTotalExpense()               
    elif choice == 'UserFavoriteItem':
        print(request.form)
        data=d.getUserFavoriteGoods(userID=request.form['userID'])
        print(data)
    if data:
       res={'code':200,'data':data}
    else:res={'code':404,'data':'null'}
    res=make_response(jsonify(res))
    res.headers['Access-Control-Allow-Origin'] = '*' 
    return res   

if __name__ == '__main__':
    app.run(host='192.168.58.177', port=8080)

